package com.geekchique.runningbuddy.activity;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentReceiver;
import android.content.ServiceConnection;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.controller.CurrentRunMapControllerImpl;
import com.geekchique.runningbuddy.controller.ICurrentRunMapController;
import com.geekchique.runningbuddy.provider.ActivityID;
import com.geekchique.runningbuddy.service.IRemoteStopClockService;
import com.geekchique.runningbuddy.service.RemoteStopClockService;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class CurrentRunMap extends MapActivity {

	private ICurrentRunMapController mapOfJogController;
	public LocationManager locationManager = null;
	protected MyIntentReceiver myIntentReceiver = null;
	final Handler mHandler = new Handler();
	private ServiceConnection connection = null;
	private static final String TAG = CurrentRunMap.class.getName();

  @Override
  public void onCreate(Bundle  icicle) {
      super.onCreate(icicle);
      this.setContentView(R.layout.current_run_map_view);
      this.myIntentReceiver = new MyIntentReceiver();

      mapOfJogController = new CurrentRunMapControllerImpl(this);

      mapOfJogController.createMapOfJogView((MapView)findViewById(R.id.map));
//
//      if(getIntent().getExtras() != null){
//    	  mapOfJogController.startTrackingRoute();
//      }
      mapOfJogController.startTrackingRoute();
  	}

	public void startStopwatchService(ServiceConnection messageConnection) {
		this.connection = messageConnection;
		startService(new Intent(CurrentRunMap.this, RemoteStopClockService.class), null);
		bindService(new Intent(IRemoteStopClockService.class.getName()), messageConnection, Context.BIND_AUTO_CREATE);
		mapOfJogController.setServiceRunning(true);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return mapOfJogController.onKeyDown(keyCode, event);
	}

	public void stopStopwatchService(ServiceConnection connection) {
		mapOfJogController.setServiceRunning(false);
		unbindService(connection);
		stopService(new Intent(CurrentRunMap.this, RemoteStopClockService.class));
	}

	class MyIntentReceiver extends IntentReceiver {

		@Override
		public void onReceiveIntent(Context context, Intent intent) {

			if(intent.getAction().equals(ActivityID.MY_LOCATION_CHANGED_ACTION)){
				mapOfJogController.updateRunningPath();
			}

			if(intent.getAction().equals(ActivityID.STOPWATCH_STARTED)){
				Log.v(TAG, "!!!!! This is the intent Action :"+ intent.getAction().toString() );
				mapOfJogController.startTrackingRoute();
			}

			if(intent.getAction().equals(ActivityID.STOPWATCH_STOPPED)){
				Log.v(TAG, "!!!!! This is the intent Action :"+ intent.getAction().toString() );
				mapOfJogController.stopTrackingRoute();
			}

		}
	}


	@Override
	protected void onFreeze(Bundle outState) {
		unbindService(connection);
		unregisterReceiver(this.myIntentReceiver);
		super.onFreeze(outState);
	}

	@Override
	protected void onResume() {
		registerReceiver(this.myIntentReceiver, new IntentFilter(ActivityID.MY_LOCATION_CHANGED_ACTION));
		registerReceiver(this.myIntentReceiver, new IntentFilter(ActivityID.STOPWATCH_STARTED));
		registerReceiver(this.myIntentReceiver, new IntentFilter(ActivityID.STOPWATCH_STOPPED));
		registerReceiver(this.myIntentReceiver, new IntentFilter("com.geekchique.runningbuddy.action.STOPTRACKER"));

		super.onResume();
	}

	@Override
	protected void onPause() {
		unbindService(connection);
		unregisterReceiver(this.myIntentReceiver);
		super.onPause();
	}


}